package com.jsonal.service.impl.manager;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsonal.common.CommonUtil;
import com.jsonal.entity.manager.Menu;
import com.jsonal.entity.manager.MenuBean;
import com.jsonal.mapper.manager.MenuMapper;
import com.jsonal.service.manager.MenuService;
import com.jsonal.vo.Result;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: jingji.lin
 * @Description:
 * @Date: 2020/6/16 9:10
 * @Version: 1.0
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    /**
     * 添加或修改菜单
     *
     * @param menuBean 菜单
     * @return res
     */
    @Override
    public Result addOrUpdateMenu(MenuBean menuBean) {
        Result result = new Result();
        // 判断id是否存在，存在则是修改，不存在则是新增
        Menu menu = new Menu();
        if (menuBean.getId() != null) {
            // 存在 是修改
            BeanUtils.copyProperties(menuBean, menu);
            int i = menuMapper.updateById(menu);
            if (i > 0) {
                result.setSuccess(true);
                return result;
            }
            result.setSuccess(false);
            return result;
        } else {
            // 不存在是新增
            BeanUtils.copyProperties(menuBean, menu);
            menu.setId(CommonUtil.getGUID());
            menu.setCreateTime(new Date());
            menu.setDelFlag(false);
            int insert = menuMapper.insert(menu);
            if (insert > 0) {
                result.setSuccess(true);
                return result;
            }
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 获取菜单列表
     *
     * @param menuBean bean
     * @return 菜单列表
     */
    @Override
    public Result getMenuList(MenuBean menuBean) {
        Result result = new Result();
        // 支持模糊查询、分页
        IPage<Menu> page = new Page<>(menuBean.getPage(), menuBean.getPageSize());
        IPage<Menu> pages = menuMapper.selectPage(page, new QueryWrapper<Menu>()
            .eq("del_flag", false)
            .orderByAsc("sort", "create_time")
        );
        result.setSuccess(true);
        result.setResults(page.getRecords());
        result.setObj(page);
        return result;
    }

    /**
     * 根据菜单 id  获取数据
     *
     * @param id 菜单 id
     * @return 该 id 的所有数据
     */
    @Override
    public Result getMenuById(String id) {
        Result result = new Result();
        Menu menu = menuMapper.selectOne(new QueryWrapper<Menu>().eq("id", id));
        if (menu == null) {
            return new Result(false, "无数据");
        }
        result.setSuccess(true);
        result.setObj(menu);
        return result;
    }

    /**
     * 获取全部菜单
     *
     * @return 全部菜单 list
     */
    @Override
    public Result getMenus() {
        Result result = new Result();
        List<MenuBean> menuList = new ArrayList<MenuBean>();
        // 获取一级菜单
        List<Menu> firstMenus = menuMapper.selectList(
            new QueryWrapper<Menu>().eq("del_flag", false).eq("sort", 1));
        for (Menu menu : firstMenus) {
            MenuBean menuBean = new MenuBean();
            BeanUtils.copyProperties(menu, menuBean);
            menuList.add(menuBean);
        }
        for (MenuBean bean : menuList) {
            bean.setChildrens(menus(bean.getId()));
        }
        result.setResults(menuList);
        result.setSuccess(true);
        return result;
    }

    /**
     * 根据 id 删除菜单 逻辑删除
     *
     * @param id 菜单 id
     * @return res
     */
    @Override
    public Result removeMenu(String id) {
        Result result = new Result();
        Menu menu = menuMapper.selectOne(new QueryWrapper<Menu>().eq("id", id));
        if (menu != null) {
            menu.setDelFlag(true);
            menuMapper.updateById(menu);
            result.setSuccess(true);
            return result;
        }
        result.setSuccess(false);
        return result;
    }

    public List<MenuBean> menus(String id) {
        // 根据一级菜单的id作为二级菜单的parentId 查询一级菜单id下的所有的子菜单
        List<Menu> childrens = menuMapper.selectList(
            new QueryWrapper<Menu>().eq("del_flag", false)
                .eq("parent_id", id));
        ArrayList<MenuBean> menuBeans = new ArrayList<>();
        for (Menu menu : childrens) {
            MenuBean bean = new MenuBean();
            BeanUtils.copyProperties(menu, bean);
            bean.setChildrens(menus(menu.getId()));
            menuBeans.add(bean);
        }
        if (childrens.size() == 0) {
            return null;
        }
        return menuBeans;
    }
}
